﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using com.guojin.mj.net.message.game;
using System;

public class VoteDissolveRoomPage : EventManager {

    //生成面板时给此赋值
    public string StartUserName;

    public Text logoText;
    public Text downTime;
    private float downTimeNum;
    public GameObject headGroup;
    public Button agreeBtn;
    public Button notAgreeBtn;
    //显示解散房间投票结果面板
    public Text resultPanel;

    void Start() {
        downTimeNum = 30;
        logoText.text = "玩家  " + StartUserName + "  申请解散房间，全部玩家同意后会被解散。";
        agreeBtn.onClick.RemoveAllListeners();
        agreeBtn.onClick.AddListener(AgreeBtnClick);
        notAgreeBtn.onClick.RemoveAllListeners();
        notAgreeBtn.onClick.AddListener(notAgreeBtnClick);
    }

    //循环显示头像
    public override void InformationSetting()
    {
        foreach (var item in GameRoomModel.GetInstance().playerDic.Values)
        {
            GameObject temp = TooL.LoadPrefab(headGroup, "VoteHead");

            temp.transform.FindChild("value").GetComponent<Image>().sprite = Resources.Load<Sprite>("BDUI/03/等待");

            temp.name = "voteHead" + item.locationIndex;

            if (item.headTexture != null)
            {
                temp.transform.FindChild("headImage").GetComponent<RawImage>().texture = item.headTexture;
            }
            else
         	{
                Debug.Log("没有找到玩家对应的头像");
            }

            //将对应头像上投票信息改为同意
            if (GameData.GetInstance().voteUserId == item.userId.ToString())
            {
                temp.transform.FindChild("value").GetComponent<Image>().sprite = Resources.Load<Sprite>("BDUI/03/同意");

                //如果玩家是自己的话，投票信息改为同意  ,投票按钮改为消失
                if (GameData.GetInstance().voteUserId == GameData.GetInstance().userID)
                {
                    temp.transform.parent.parent.parent.FindChild("group").gameObject.SetActive(false);
                }

                item.isVoteResult = true;
                item.VoteResult = true;
            }
        }
    }

    public void ShowResultPanel(bool  result, int locationIndex)
    {
        //if (result)
        //{
        //    resultPanel.text = "解散房间投票成功!";
        //    resultPanel.gameObject.SetActive(true);
        //}
        //else
        //{
        //    resultPanel.text = "玩家"+ GameRoomModel.GetInstance().playerDic[locationIndex].userName + "拒绝解散房间!";
        //    resultPanel.gameObject.SetActive(true);
        //}

        Invoke("CloseBtnClick", 1f);
    }
    

    bool isAgree = true;
    bool isNotAgree = true;
    float agreeTimer = 0;
    float notAgreeTimer = 0;
    float interval = 3;

    //倒计时30S
    void Update() {
        if (downTimeNum > 0.5)
        {
            downTimeNum -= Time.deltaTime;
            downTime.text = Math.Floor(downTimeNum).ToString();
        }
        else
        {
            downTimeNum = 0;
            DestroyImmediate(gameObject);
        }

        if (!isAgree)
        {
            agreeTimer += Time.deltaTime;
            if (agreeTimer > interval)
            {
                agreeTimer = 0;
                isAgree = true;
            }
        }

        if (!isNotAgree)
        {
            notAgreeTimer += Time.deltaTime;
            if (notAgreeTimer > interval)
            {
                notAgreeTimer = 0;
                isNotAgree = true;
            }
        }
    }

    public void AgreeBtnClick()
    {
        if (isAgree)
        {
            MessageManager.GetInstance().SendMsg1222(true, int.Parse( GameData.GetInstance().userID));
            isAgree = false;
        }
        else
        {
            Debug.Log("已经发送过确定投票了");
        }
    }

    public void notAgreeBtnClick()
    {
        if (isNotAgree)
        {
            MessageManager.GetInstance().SendMsg1222(false,int.Parse( GameData.GetInstance().userID));
            isNotAgree = false;
        }
        else
        {
            Debug.Log("已经发送过取消投票");
        }
    }
}
